// pages/my/vip/index.js
const app=getApp();
const getVipCard=require("../../controllers/ControllerVip").controller.getVipCard;
const myInformation=require("../../controllers/ControllerVip").controller.myInformation;
const openCard=require("../../controllers/ControllerVip").controller.openCard;
const payMent=require("../../controllers/ControllerVip").controller.payMent;
const Controllers = require('../../controllers/Controller.js').controller;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    globalStype:'',
    value:{
      
    },
    cardList:[],
    yellow_bg:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210915/887756367236956160.png",
    white_bg:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210915/887756346697449472.png",
    vip_bg:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20211126/913795093343764480.png",
    vip:0,
    selectId:'',
    accountId:'',
    shopId:'',
    backbool:true,
    type:'',
    types:"",
    showModalStatus:false,
    staffList:[],
    selectIndex:'',
    staffId:"",
    staffName:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    this.setData({globalStype:app.globalData.globalStype})
    if (option.shopId) {
      this.setData({
        shopId: option.shopId
      })
      app.globalData.shopId = option.shopId
      wx.setStorageSync('shopId', option.shopId)
    }
    if(getCurrentPages().length<=1){
      this.setData({
        backbool:false
      })
    }else{
      this.setData({
        backbool:true
      })
    }
    if(wx.getStorageSync('shopId')){
      this.setData({
        shopId:wx.getStorageSync('shopId')
      })
    }
    if(option.type){
      this.setData({
        type:option.type
      })
    }
    if(option.types){
      this.setData({
        types:option.types
      })
    }
  },
  onShow(){
    if (wx.getStorageSync('usertoken')) {
        // 获取vip列表
        this.myInformation()
        if(this.data.invitationAccountId){
          this.invitationPerson()
        }
        this.getCardList();
        this.getStaffList();
        this.getMyInformation();
    }
    //无usertoken，等待login登录回调
    app.usertokenReadyCallback = res => {
      if(res.data.sessionId){
        this.myInformation()
          if(this.data.invitationAccountId){
            this.invitationPerson()
          }
        // 获取vip列表
        this.getCardList();
        this.getStaffList();
        this.getMyInformation();
      }
    }
    
  },
  //获取vipCard列表
   getCardList(){
     let shopId=app.globalData.shopId;
     shopId=parseInt(shopId);
     this.setData({
      shopId:shopId
     })
    getVipCard({shopId:shopId,type:this.data.type,types:this.data.types}).then((res)=>{
      this.setData({
        cardList:res.data
      })
    });
  },

  //获取员工列表
  getStaffList(){
    let shopId=wx.getStorageSync('shopId')
    Controllers.getStaffList({shopId:shopId}).then(res=>{
      res.data.unshift({id:''})
      this.setData({
        staffList:res.data
      })
    })
  },
  //跳转我的卡片页
  jumpToVip_myCard(){
    wx.navigateTo({
      url: '/pages/my/vip/vip_myCard/index?type='+this.data.type,
    })
  },
  //进入使用说明页
  jumpVip_explain(e){
    let index=e.currentTarget.dataset.index;
    app.globalData.vipDesc=this.data.cardList[index].desc;
    wx.navigateTo({
      url: '/pages/my/vip/vip_explain/index',
    })
  },

  //获取自己详细信息
  getMyInformation(){
    let shopId=app.globalData.shopId;
    myInformation({shopId:shopId}).then(res=>{
      this.setData({
        vip:res.data.accountRelation.vip,
        accountId:res.data.accountRelation.accountId
      })
    })
  },

  //选中vip卡
  selectCard(e){
    let id=e.currentTarget.dataset.id;
    this.setData({
      selectId:id
    })
    console.log("触发");
    console.log(this.data.selectId);
  },

  //跳转详情
  jumpDetail(event){
    let id=event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/my/vip/info/VIPCardInfo?id='+id+'&notQC=1',
    });
    
  },
  //pages/my/vip/info/VIPCardInfo

  // 打开弹窗
  isOpenCard(){
    let that=this
    if(that.data.selectId!=''){
    wx.showModal({
      title: '提示',
      content: '是否开卡？',
      success (res) {
        if (res.confirm) {
          that.setData({
            showModalStatus:true
          })  
        }
      }
    })
    }else{
      wx.showToast({
        icon:"none",
        title: '请选择会员卡',
        duration:2500
      })
    }
  },
  //选择员工
  selectStaff(e) {
    let index = e.currentTarget.dataset.index
    console.log(e)
    if (index === this.data.selectIndex) {
      this.setData({
        selectIndex: "",
        staffId: "",
        staffName: ""
      })
    } else {
      this.setData({
        selectIndex: index,
        staffId: this.data.staffList[index].id,
        staffName: this.data.staffList[index].nick
      })
    }
  },

  // //确定员工
  // defineStaff(){
  //   if(this.data.selectIndex||this.data.selectIndex===0){
  //     this.setData({
  //       staffId:this.data.list[this.data.selectIndex].id,
  //       staffName:this.data.list[this.data.selectIndex].nick
  //     })
  //   }
  // },
  // 隐藏弹框
  hideModal: function () {
    this.setData({
      showModalStatus: false,
      selectIndex: "",
      staffId: "",
      staffName: ""
    })
  },
  //跳转员工详情
  selectStaffDetail(e) {
    let index = e.currentTarget.dataset.index
    if (index == 0) return
    let value = this.data.staffList[index]
    wx.navigateTo({
      url: '/pages/selecStaffDetail/selecStaffDetail?staffDetail=' + encodeURIComponent(JSON.stringify(value)),
    })
  },
//会员开卡
  openCard(){
    let shopId=app.globalData.shopId;
    let vipId=this.data.selectId;
    let shopAccountId=this.data.staffId;
    let selectIndex=this.data.selectIndex;

    if(!(selectIndex || selectIndex === 0)){
      wx.showToast({
        title: '请正确选择跟单员工',
        icon:'none'
      })
      return 
    }
    this.hideModal()
    console.log(shopAccountId);

    openCard({shopId:shopId,vipId:vipId,shopAccountId}).then(res=>{
      if(res.code==1){
        let orderNo=res.data.orderNo;
        let openId=app.globalData.openId
        payMent({shopId:shopId,orderNo:orderNo,openId:openId,frontUrl:"mp:///pages/Index/Index"}).then(res=>{
            if(res.code==1){
              // let url=res.data;
              // url=encodeURIComponent(url);
              // console.log(url);
              // wx.navigateTo({
              //   url: '/pages/weixinlink/weixinlink?url='+url,
              // })
              // console.log(url);
              let data=JSON.parse(res.data);
              console.log(data)
              wx.requestPayment({
                nonceStr: data.nonceStr,
                package: data.package,
                paySign: data.paySign,
                timeStamp: data.timeStamp,
                signType: data.signType,
                success(res){
                  console.log(res)
                },
                fail(res){
                  console.log(res)
                }
              })
            }
        })
      }else{
        wx.showToast({
          icon:"none",
          title: '开卡失败',
          duration:2000
        })
      }
    })
  },
  onShareAppMessage: function (e) {
    let obj = {
      // title: this.data.shopDetailsdata.shopName,
      path:'/pages/my/vip/index?shopId=' + this.data.shopId+"&invitationAccountId="+this.data.myInformationdata.accountId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  },
  onShareTimeline: function (e) {
    let obj = {
      title: '',
      path:'/pages/Mall/Grouplists?shopId=' + this.data.shopId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  },
  backindex(e){
    console.log(e)
    if (wx.getStorageSync('usertoken')) {
        wx.navigateTo({
          url:'/pages/Index/Index'
        })
    }else{
      app.GetLogin();
    }
  },

  myInformation(){
    //获取数据
    Controllers.myInformation({shopId:this.data.shopId}).then(res => {
      this.setData({
        myInformationdata:res.data.accountRelation
      })
      app.globalData.myshopinfo=res.data.accountRelation
    })
  },

  //绑定上级
  invitationPerson(){
    let value={}
    value.invitationAccountId=this.data.invitationAccountId
    value.shopId=wx.getStorageSync('shopId')
    Controllers.invitationPerson(value).then(res=>{
        
    })
  }
})